package 
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	import ginger.utils.NetUtil;
	import hwsoft.webservice_2_0.utils.ParseUtil;
	import UILib.Background;
	import UILib.Menu;
	import UILib.Panel1;
	import UILib.Panel2;
	import UILib.Panel3;
	import UILib.Panel4;
	import UILib.Panel5;
	import UILib.Slider;
	import caurina.transitions.Tweener;
	
	
	/**
	 * ...
	 * @author Ning
	 */
	public class Main extends Sprite
	{
		private var _menu:Menu;
		private var _silder:Slider;
		private var _silderIndex:int;
		private var _panels:Array;
		private var _link:Array;
		private var _curIndex:int;
		public function Main():void 
		{
			if (stage) onAddedToStage();
			else addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		private function onAddedToStage(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			// entry point
			setTimeout(init, 200);
		}
		
		private function init():void
		{
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			
			var obj:Object = NetUtil.getFlashVarsParam(this.root, 'link');
			var link:String = obj == null?'asset/link.xml':String(obj);
			
			var urlloader:URLLoader = new URLLoader();
			urlloader.addEventListener(Event.COMPLETE, onComplete);
			urlloader.load(new URLRequest(String(link)));
			
		}
		private function onComplete(evt:Event):void
		{
			_link = ParseUtil.parse(XML(evt.target.data), NavVO);
			
			var bg:Background = new Background;
			addChild(bg);
			
			_silder = new Slider();
			_silder.y = 37;
			addChild(_silder);
			
			_menu = new Menu();
			addChild(_menu);
			_menu.x = 138;
			_menu.y = 38;
			
			for (var i:int = 0; i < _menu.numChildren; i++ )
			{
				var menu:MovieClip = _menu.getChildAt(i) as MovieClip;
				menu.buttonMode = true;
				menu.graphics.beginFill(0xffffff, 0);
				menu.graphics.drawRect(0, 0, menu.width+35, menu.height+20);
				menu.addEventListener(MouseEvent.ROLL_OVER, onOver);
				menu.addEventListener(MouseEvent.ROLL_OUT, onOut);
				menu.addEventListener(MouseEvent.CLICK, onMenuClick);
			}
			
			_panels = [new Panel1,new Panel2, new Panel3, new Panel4,new Panel5];
			for (i = 0; i < _panels.length; i++ )
			{
				var panel:Sprite = _panels[i] as Sprite;
				panel.y = 30;
				panel.x = 3;
				for (var j:int = 0; j < panel.numChildren; j++ )
				{
					var btn:SimpleButton = panel.getChildAt(j) as SimpleButton;
					if(btn)
					    btn.addEventListener(MouseEvent.CLICK, onBtnClick);
				}
			}
			
			var obj:Object = NetUtil.getFlashVarsParam(this.root, 'silderIndex');
			if (obj != null)
			    _silderIndex = int(obj);
			_silder.x = getSilderPosXAt(_silderIndex);
		}
		private function onOver(evt:MouseEvent):void
		{
			var menu:MovieClip = evt.target as MovieClip;
			var index:int = _menu.getChildIndex(menu);
			_curIndex = index;
			var x:Number = getSilderPosXAt(index);
			Tweener.addTween(_silder, { time:.5, x:x } );
			
			var panel:Sprite = getPanelAt(index);
			if (panel) {
				Tweener.removeTweens(panel);
				if (!menu.contains(panel))
				    menu.addChild(panel);
				panel.alpha = 0;
				Tweener.addTween(panel, {time:.5,alpha:1} );
			}
		}
		private function onOut(evt:MouseEvent):void
		{
			var x:Number = getSilderPosXAt(_silderIndex);
			Tweener.addTween(_silder, { time:.8, x:x } );
			_curIndex = _silderIndex;
			var menu:MovieClip = evt.target as MovieClip;
			var index:int = _menu.getChildIndex(menu);
			
			var panel:Sprite = getPanelAt(index);
			if (panel) {
				Tweener.addTween(panel, {time:.5,alpha:0,onComplete:onComplete} );
			}
			function onComplete():void
			{
				if (menu.contains(this))
				    menu.removeChild(this);
			}
		}
		private function onMenuClick(evt:MouseEvent):void
		{
			var menu:MovieClip = evt.target as MovieClip;
			if (menu == null)
			    return;
			var index:int = _menu.getChildIndex(menu);
			var link:String = NavVO(_link[index]).LINK;
			var window:String = getWindow(NavVO(_link[index]).WINDOW);
		//	trace(link)
			navigateToURL(new URLRequest(link), window);
		}
		private function onBtnClick(evt:MouseEvent):void
		{
			var btn:SimpleButton = evt.target as SimpleButton;
			var panel:Sprite = getPanelAt(_curIndex);
			var index:int = panel.getChildIndex(btn);
			
			//链接数据间隔数
			var temIndex:int = 0;
			switch(_curIndex)
			{
				case 1:
					temIndex = 7;
					break;
				case 2:
					temIndex = 9;
					break
				case 3:
					temIndex = 14;
					break;
				case 4:
					temIndex = 19;
					break;
				case 5:
					temIndex = 24;
					break;
			}
			var link:String = NavVO(_link[temIndex + index - 1]).LINK;
			var window:String = getWindow(NavVO(_link[temIndex + index - 1]).WINDOW);
		//	trace(index+' '+link)
			navigateToURL(new URLRequest(link), window);
			
		}
		private function getSilderPosXAt(index:int):Number
		{
			var menu:MovieClip = _menu.getChildAt(index) as MovieClip;
			if (index != 0)
			    var x:Number = menu.x + 144;
			else 
			    x = 121;
			return x;
		}
		private function getPanelAt(index:int):Sprite
		{
			var panel:Sprite;
			switch(index)
			{
				case 1:
					panel = _panels[0];
					break;
				case 2:
					panel = _panels[4];
					break
				case 3:
					panel = _panels[1];
					break
				case 4:
					panel = _panels[2];
					break;
				case 5:
					panel = _panels[3];
					break;
			}
			return panel;
		}
		
		private function getWindow(val:String):String
		{
			var w:String = '_self';
			switch(val)
			{
				case '0':
					w = '_self';
				break;
			    case '1':
				    w = '_blank';
				break;
			}
			return w;
		}
	}
}